﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once

#include <QDialog>
#include "UIPlatform.h"
#include "IModelViewDlg.h"
#include "QFamilyTreeWidgetUtil.h" 

namespace gcmp
{
    class IUiDocument;
    class IUiView;
    class IElementsChangeEventHandler;
    class IUndoRedoEventHandler;
    class ITransactionEventHandler;
    class IDocument;
    class IFamily;
    class IDock;
}

namespace Ui
{
    class ModelViewDlg;
}

class QMenu;

namespace Sample
{
    class QFamilyTreeWidget;
    class QMeshTreeWidget;
    class QImportedObjectTreeWidget;
    class QGroupObjectTreeWidget;

    class UiLayoutManager;

    // 资源管理器，管理视图，构件，导入文件和组等
    class UIPLATFORM_EXPORT ModelViewDlg : public QDialog, public IModelViewDlg
    {
        Q_OBJECT

    public:
        ModelViewDlg(const ModelViewDlgInput& input);
        virtual ~ModelViewDlg();

#pragma region IModelViewDlg 接口
    public:
        virtual void Init(gcmp::IUiDocument* pUIDoc) override;

        virtual void SetEnabled(bool bEnable) override;
        virtual void UpdateStatus() override;
#pragma endregion IModelViewDlg 接口

    public:
        QTreeWidgetItem* GetSelectedPaper();
        QTreeWidgetItem* GetSelectedViewPort();
        UiLayoutManager* GetUiLayoutManager() const;

    private:
        //用缓存的UiDoc刷新资源管理器下所有内容
        void Refresh();

        //在视图管理器界面上高亮所设视图
        virtual void HighlightSelectedModelView(const gcmp::IUiView* pUIView);

        void HighLightSelectedViewport(QTreeWidgetItem* pViewportItem);

        virtual void UpdateFamiliesTreeView(gcmp::IDocument* pDoc);

        virtual void UpdateImportedObjectsTreeView(gcmp::IDocument* pDoc);

        virtual void UpdateGroupObjectsTreeView(gcmp::IDocument* pDoc);

        void InitFamilyTree();

        void InitImportedObjectTree();

        void InitGroupObjectTree();

        void InitPaperTree();
        void UpdatePaperTreeView(gcmp::IDocument* pDoc);
        QTreeWidgetItem* GetRootPaperItem();

        QTreeWidgetItem* InitViewType(const std::wstring& viewType);

        void UpdateModelViewsTree(gcmp::IUiDocument* pUIDoc);

        void ExpandAllModelViewItems();

        void ClearModelViewsItem();

        QSize sizeHint() const;

        QTreeWidgetItem* GetRootFamilyItem();

        void AttachEventHandler(gcmp::IUiDocument* pUIDoc);
        void DetachEventHandler(gcmp::IUiDocument* pUIDoc);

        void UpdateFamilyTreeItemContextMenu(const QList<QTreeWidgetItem*>& selectedItem);

        void UpdateImportedObjectTreeItemContextMenu(const QList<QTreeWidgetItem*>& selectedItem);

        void UpdateGroupObjectTreeItemContextMenu(const QList<QTreeWidgetItem*>& selectedItem);

    protected:
        virtual void keyPressEvent(QKeyEvent *event) override;
        virtual void leaveEvent(QEvent *) override;
        virtual void showEvent(QShowEvent *event)override;
        virtual void contextMenuEvent(QContextMenuEvent* event) override;

    private slots:
        void CreateFamilyInstance();
        void RenameFamily();
        void EditFamily();
        void LoadFamily();
        void LoadSubFamily();
        void SaveAsFamily();
        void RemoveFamily();
        void OnModelViewTreeItemDoubleClicked(QTreeWidgetItem *item, int column);
        void RemoveSelectedModelView();
        void RemoveSelectedPaperOrViewPort();
        void RemoveImportedObject();
        void ViewportDeactivate();
        void CreateGroup();
        void DeleteGroupType();
        void RenameGroupType();
        void ExportGroupType();
        void EditGroupType();
        void CreateOpen3DView();
        void CreateOpenPlanView();
        void CreateOpenSectionView();
        void CreateLayout();
    private:
        friend class ModelViewDlgUiDocumentEventHandler;
        friend class ModelViewDlgElementChangeEventHandler;
        friend class ModelViewDlgUiViewEventHandler;
        friend class ModelViewDlgUndoRedoEventHandler;
        friend class ModelViewDlgTransactionEventHandler;
        friend class QTreeWidgetPaper;
        friend class UpdateUiViewLayoutEventHandler;
        friend class UpdateUiViewViewportEventHandler;

    private:
        gcmp::OwnerPtr<gcmp::IElementsChangeEventHandler>  m_pElementChangeEventHandler;    // 文档Element变化事件处理器
        gcmp::OwnerPtr<gcmp::IUndoRedoEventHandler>  m_pUndoRedoEventHandler;    // undoRedo事件处理器
        gcmp::OwnerPtr<gcmp::ITransactionEventHandler>  m_pTransactionEventHandler;    // Transaction事件处理器

        gcmp::OwnerPtr<Ui::ModelViewDlg> m_ui;

        QFamilyTreeWidget* m_pFamilyTree;    // （与视图树分开时）族树的挂件
        QImportedObjectTreeWidget* m_pImportedObjectTree;
        QGroupObjectTreeWidget* m_pGroupObjectTree;
        QIconMap m_familyIcons;    // 缓存图标，以避免每次从资源加载
        QMenu* m_treeMenu;    // 族树的右键菜单

        int     m_uiDocID;      // ui文档Id
        bool    m_isNeedUpdate; // 是否需要更新(用于连续修改延时更新）
        bool m_bShowFamilyManagementMenuItem; // 是否在族右键时显示菜单

        // 绑定到族树右键菜单的命令
        gcmp::OwnerPtr<UiLayoutManager> m_opUiLayoutManager;

        gcmp::OwnerPtr<UpdateUiViewLayoutEventHandler>  m_opLayoutEventHandler;     //layout事件处理器
        gcmp::OwnerPtr<UpdateUiViewViewportEventHandler>  m_opViewportEventHandler;      //viewpotrt事件处理器
    };
}

